﻿#region 引用

using System;
using System.Collections.ObjectModel;
using System.Data.Entity.Migrations;
using System.Linq;
using Sohobloo.SoCommon.CommonExtension;
using Sohobloo.SoCommon.Security;
using Sohobloo.SoMcCoffee.Data;
using Mojito.Entity;
using Sohobloo.SoMcCoffee.Entity;

#endregion

namespace Mojito.Data.Migrations
{
    internal sealed class Configuration : DbMigrationsConfiguration<EfDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(EfDbContext context)
        {
            var systemDate = new DateTime(1900, 1, 1);
            const string systemOperator = "System";
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            // 默认收支账户
            var defaultAccount = context.Set<Account>().SingleOrDefault(a => a.DataStatus == DataStatus.System) ?? new Account
            {
                Id = Guid.NewGuid(),
                CreateTime = systemDate,
                CreateUser = systemOperator,
                UpdateTime = systemDate,
                UpdateUser = systemOperator,
                DataStatus = DataStatus.System,
                SortIndex = systemDate.Ticks,
            };
            defaultAccount.AccountName = @"[默认账户]";
            context.Set<Account>().AddOrUpdate(defaultAccount);

            // 默认店铺设置
            var shop = context.Set<Shop>().SingleOrDefault() ?? new Shop
            {
                Id = Guid.NewGuid(),
                CreateTime = systemDate,
                CreateUser = systemOperator,
                UpdateTime = systemDate,
                UpdateUser = systemOperator,
                SortIndex = systemDate.Ticks
            };
            shop.TimeZone = TimeZoneInfo.Local.Id; // 当前系统时区
            shop.DailySettlementTime = new TimeSpan(0, 6, 0, 0).Ticks; // 早上6点
            shop.DefaultAccountId = defaultAccount.Id;
            shop.ConfuseOrderNumber = false;
            context.Set<Shop>().AddOrUpdate(shop);

            context.SaveChanges();
        }
    }
}